using System;

namespace DataDynamics.ActiveReports.Samples
{
	/// <summary>
	/// GetDBPath is a helper class used to locate the Northwind sample database on the computer.
	/// </summary>
	/// <author>Data Dynamics, Ltd.</author>
	sealed class GetDBPath
	{
		private GetDBPath()
		{
		}


		private static string registryKey = @"SOFTWARE\\Data Dynamics\\ActiveReports for .NET 3.0\\SampleDB";
		/// <summary>
		/// Used to get the path to the Sample database installed with ActiveReports
		/// </summary>
		/// <author>Data Dynamics, Ltd.</author>
		/// <returns>String variable with the path to the Sample Database</returns>
		public static string GetPath()
		{
			string sPath = null;
			try
			{
				// Create a RegistryKey value to retrieve the localmachine entries, and open the subkey that points to the database
				Microsoft.Win32.RegistryKey hklm = Microsoft.Win32.Registry.LocalMachine;
				Microsoft.Win32.RegistryKey sampleDbKey = hklm.OpenSubKey(registryKey);
				sPath = sampleDbKey.GetValue("") as string;
			}
			catch (System.Exception eReg)
			{
				// problem occured with attempting to read the local registry (No access rights for current user?)
				throw new System.ApplicationException("An error occured accessing the registry key " + registryKey + ".", eReg);
			}
			if (sPath == null) 
			{
				// we couldn't read the registry
				throw new System.ApplicationException("Unable to access the sample database registry value " + registryKey + ".");
			}
			return sPath;			
		}

		/// <summary>
		/// Used to get the full connection string to the Sample Database installed with ActiveReports
		/// </summary>
		/// <author>Data Dynamics, Ltd.</author>
		/// <returns>String variable with the full connection string to the Sample Database</returns>
		public static string GetNwindConnectString
		{
			get
			{
				return @"Provider=Microsoft.Jet.OLEDB.4.0;Persist Security Info=False;Data Source=" + GetDBPath.GetPath() + "\\nwind.mdb";
			}
		}
	}
}